package com.edu.edumanager.controller;

import com.edu.edumanager.bean.EduTeacher;
import com.edu.edumanager.service.TeacherService;
import com.edu.edumanager.vo.EduResult;
import com.edu.edumanager.vo.EduSubjectList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-02 20:59
 **/
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 根据id查询讲师
     *
     * @param teacherId
     * @return 结果集
     */
    @RequestMapping("/{teacherId}")
    public EduResult findTeacher(@PathVariable Integer teacherId) {

        EduTeacher eduTeacher = teacherService.findTeacher(teacherId);
        return EduResult.ok().data("teacher", eduTeacher);
    };



    /**
     * 上传用户头像
     *
     * @param file
     * @return
     */
    @RequestMapping("/uploadAvatar")
    public EduResult uploadAvatar(MultipartFile file) {
        EduResult eduResult = teacherService.uploadAvatar(file);
        return eduResult;
    };



    /**
     * 添加讲师
     *
     * @param teacher
     * @return
     */
    @PostMapping("/addTeacher")
    public EduResult addTeacher(@Valid @RequestBody EduTeacher teacher) {
        int count = teacherService.addTeacher(teacher);
        return EduResult.ok().data("count", count);
    }

    /**
     * 逻辑删除讲师
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteTeacher")
    public EduResult deleteTeacher(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        int count = teacherService.deleteTeacher(ids);
        return EduResult.ok().data("count", count);
    }

    /**
     * 逻辑修改讲师 讲师必须为正常状态
     *
     * @param eduTeacher
     * @return
     */
    @PostMapping("/updateTeacher")
    public EduResult updateTeacher(@Valid @RequestBody EduTeacher eduTeacher) {
        int count = teacherService.updateTeacher(eduTeacher);
        return EduResult.ok().data("count", count);
    }

    /**
     * 查询讲师列表展示
     *
     * @param teacher
     * @return
     */
    @PostMapping("/conditionsQuery/{currentPage}/{limit}")
    public EduResult conditionsQuery(@PathVariable @Min(value = 1, message = "页面最小为1") @NotNull(message = "currentPage 不能为空") Integer currentPage,
                                     @PathVariable @Min(value = 1, message = "limit最小为1") @NotNull(message = "currentPage 不能为空") Integer limit, EduTeacher teacher) {
        System.out.println(teacher);
        EduResult eduResult = teacherService.conditionsQuery(currentPage, limit, teacher);

        return eduResult;
    }

    /**
     * 查询所有讲师和讲师Id
     *
     * @param
     * @return
     */
    @PostMapping("/getTeacherList")
    public EduResult getTeacherList() {
        List<EduSubjectList>  list = teacherService.findTeacherAll();
        return EduResult.ok().data("teacher", list);
    }
}
